#!/bin/bash                                                                                                           
# File Name: clean_binlog.sh
# Author: moshan
# mail: mo_shan@yeah.net
# Created Time: 2018-10-15 11:39:53
# Function: clean data for mysql binlog
#########################################################################
save_days=30
work_dir="/home/moshan/script"
log_dir="${work_dir}/log/$(date +%F)"
log_file="${log_dir}/remove.log"
list_file="${log_dir}/binlog.list"
binlog_dir="/mfw_rundata/backup/db/mysql/binlog"
[ ! -d "${log_dir}" ] && mkdir -p ${log_dir}
find ${binlog_dir} -mtime +${save_days} -type f -name "*"|xargs -i ls -l {} > ${list_file}
#find ${binlog_dir} -mtime +${save_days} -type f -name "*"|xargs -i rm -f {} #可直接将满足条件的文件全部删除
binlog_file_array=($(awk '{print $NF}' ${list_file}))
for ((i=0;i<${#binlog_file_array[@]};i++))
do
    if [ -f "${binlog_file_array[i]}" ]
    then
       rm -f ${binlog_file_array[i]}
       if [ $? -eq 0 ]
       then
            echo "[$(date "+%F %H:%M:%S")] [OK] Already deleted:${binlog_file_array[i]}" >>${log_file}
        else
            echo "[$(date "+%F %H:%M:%S")] [ERROR] Deletion error:${binlog_file_array[i]}" >> ${log_file}
        fi
    else
        echo "[$(date "+%F %H:%M:%S")] [ERROR] ${binlog_file_array[i]}:No such file." >> ${log_file}
    fi
    sleep 2
done
